$(function(){
    $.post($.webPath() + '/base/sjzdsy/get_sjzdz', {'bm': 'cd'}, function (cddata) {
        kcmxGrid(cddata);
    });
    $("#kc_bjgs").combotree({
        url:$.webPath()+'/organ/gsjg/gsjg_tree?type=1',
        panelHeight:'200',
        lines:'true',
        readonly:true,
        loadFilter: function (rows) {
            return convert(rows);
        }
    });
});
//新增库存明细
$("#kcbj_kc_toolbar [name='xz_kcbj_kc']").click(function () {
    searchcodewindow(kcmx_searchWindowOKbtnHandler);
});

$("#kcbj_kc_toolbar [name='kcbj_sckc']").click(function () {
    $.messager.confirm('提示', '确定生成?', function (r) {
        if (r) {
            $.post($.webPath() + '/xsgl/kcbj/scKc',function(data){
                if(data.statusCode == 200){
                    $("#kcbj_kcmx").edatagrid('reload');
                } else {
                    $.messager.alert('提示', data.message, 'error');
                }
            },'json');
        }
    });
});
//删除库存明细
$("#kcbj_kc_toolbar [name='sc_kcbj_kc']").click(function () {
    var rows = $("#kcbj_kcmx").edatagrid('getSelected');
    var index = $("#kcbj_kcmx").edatagrid('getRowIndex', rows);
    if (index>-1) {
        $.messager.confirm('提示', '是否确定删除合同明细?', function (r) {
            if (r) {
                if(rows.id){
                    $.post($.webPath() + '/xsgl/kcbj/delete_kc',{'kc.id':rows.id},function(data){
                        if(data.statusCode == 200){
                            $("#kcbj_kcmx").edatagrid('deleteRow', index);
                        } else {
                            $.messager.alert('提示', data.message, 'error');
                        }
                    },'json');
                }else {
                    $("#kcbj_kcmx").edatagrid('deleteRow', index);
                }
            }
        });
    }
    $("#kcbj_kcmx").edatagrid('resize');
});
//查询物资代码确定按钮回调函数
function kcmx_searchWindowOKbtnHandler(data) {
    var rowdatas = $.map(data, function (n, i) {
        return {
            'gg.id': n.id,
            'gg.name': n.name,
            'gg.cz': n.cz,
            'gg.ggxh': n.ggxh,
            'gg.dw': n.dw,
            'cd.id': null
        };
    });
    $.each(rowdatas, function () {
        $("#kcbj_kcmx").edatagrid('insertRow', {index:0,row:this});
    });
}

function kcmxGrid(cdOptions) {
    $("#kcbj_kcmx").edatagrid({
        url: $.webPath() + '/xsgl/kcbj/kcglList?bjgsId=' + $("#kc_bjgs").combotree("getValue") || '',
        fitColumns: true,
        singleSelect: true,
        fit: true,
        checkbox: true,
        checkOnSelect: false,
        selectOnCheck: false,
        rownumbers: true,
//        pagination:true,
        toolbar: "#kcbj_kc_toolbar",
        columns: [
            [
                {field: 'gg.name', title: '物资名称', width: 50},
                {field: 'gg.cz', title: '材质', width: 30},
                {field: 'gg.ggxh', title: '规格型号', width: 30},
                {field: 'gg.dw', title: '单位', width: 30},
                {field: 'cd.id', editor: {type: 'combobox', options: {
                    data: cdOptions,
                    valueField: 'id',
                    textField: 'z',
                    multiple: false,
                    panelHeight: '200'
                }
                }, formatter: function (value) {
                    for (var i = 0; i < cdOptions.length; i++) {
                        if (cdOptions[i].id == value) return cdOptions[i].z;
                    }
                    return value;
                }, title: '产地', width: 50}
            ]
        ],
        onSelect: function (rowIndex, rowData) {
            $("#kcbj_kcmx").edatagrid('editRow', rowIndex);
        }
    });
}
